package Service;

import com.example.SSMDemo.Entity.News;
import com.example.SSMDemo.Mapper.NewsMapper;
import org.apache.catalina.mapper.Mapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class NewsService {

    @Autowired
    NewsMapper newsMapper;

    public boolean AddNews(News news){
//        String sql="INSERT INTO news ( category, title,content,author, newsdate)" +
//                " VALUES( '"+news.getCategory()+"', '"+news.getTitle()+"','"+news.getContent()+"', " +
//                "'"+news.getAuthor()+"','"+String.valueOf(news.getNewsdate())+"');";

        newsMapper.insert(news);

        return true;
    }

    public boolean DeleteNews(int idnews){
//        String sql ="delete from news where idnews="+Integer.toString(idnews);

        newsMapper.deleteByPrimaryKey(idnews);

        return true;// rs;
    }

    public boolean UpdateNews(News news) {
//        String sql ="update news set category='"+news.getCategory()+"',title='"+news.getTitle()
//                +"',content='"+news.getContent()+"',author='"+news.getAuthor()+"'," +
//                "newsdate='"+String.valueOf(news.getNewsdate())+"' where idnews="+news.getIdnews()+"";

        newsMapper.updateByPrimaryKey(news);


        return true;
    }

    public List<News> QueryNews() throws SQLException {
//        String sql="Select * from news";
//        ResultSet rs= SqlHelper.executeQuery(sql);
//        List<News> lsNews=new ArrayList<News>();
//        while (rs.next())
//        {
//            News news=new News();
//            news.setIdnews(rs.getInt(1));
//            news.setCategory(rs.getString(2));
//            news.setTitle(rs.getString(3));
//            news.setContent(rs.getString(4));
//            news.setAuthor(rs.getString(5));
//            news.setNewsdate(rs.getDate(6));
//            lsNews.add(news);
//        }
        return newsMapper.selectAll();
    }

    public News GetNews(int idnews) throws SQLException {
        String sql="Select * from news where idnews='"+Integer.toString(idnews)+"'";
//        ResultSet rs= SqlHelper.executeQuery(sql);
//        if (rs.next())
//        {
//            News news=new News();
//            news.setIdnews(rs.getInt(1));
//            news.setCategory(rs.getString(2));
//            news.setTitle(rs.getString(3));
//            news.setContent(rs.getString(4));
//            news.setAuthor(rs.getString(5));
//            news.setNewsdate(rs.getDate(6));
//            return news;
//        }
        return newsMapper.selectByPrimaryKey(idnews);
    }
}
